
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.dto.DictExamClassDto;
import com.rt.schedulebase.dto.DictRoleFunctionDto;
import com.rt.schedulebase.dto.DictSysparamsDto;
import com.rt.schedulebase.entity.DictExamClass;
import com.rt.schedulebase.entity.DictRoleFunction;
import com.rt.schedulebase.mapper.DictExamClassMapper;
import com.rt.schedulebase.mapper.DictSysparamsMapper;
import com.rt.schedulenew.api.IDictExamClassService;
import com.rt.schedulenew.api.IDictRoleFunctionService;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictExamClassServiceImpl extends ServiceImpl<DictExamClassMapper, DictExamClass> implements IDictExamClassService {

    @Autowired
    IDictRoleFunctionService dictRoleFunctionService;
    @Autowired
    DictSysparamsMapper dictSysparamsMapper;

    @Override
    public List<DictExamClass> getList(DictExamClassDto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("EXAM_CLASS_NAME", dto.getExamClassName());
        map.put("COMMON_NAME", dto.getCommonName());
        map.put("CLASS_ATTR", dto.getClassAttr());
        QueryWrapper<DictExamClass> qw = (QueryWrapper<DictExamClass>) new QueryWrapper();
        qw.allEq((Map) map, false);
        qw.orderByAsc("EXAM_CLASS_NAME");
        List<DictExamClass> list = (List<DictExamClass>) ((DictExamClassMapper) baseMapper).selectList((Wrapper) qw);
        return list;
    }

    @Override
    public List<DictExamClass> getListAll() {
        return (List<DictExamClass>) ((DictExamClassMapper) baseMapper).selectList((Wrapper) null);
    }

    @Override
    public DictExamClass getInfo(DictExamClassDto dto) {
        QueryWrapper<DictExamClass> qw = (QueryWrapper<DictExamClass>) new QueryWrapper();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("EXAM_CLASS_NAME", dto.getExamClassName());
        map.put("COMMON_NAME", dto.getCommonName());
        map.put("CLASS_ATTR", dto.getClassAttr());
        qw.allEq((Map) map, false);
        return (DictExamClass) ((DictExamClassMapper) baseMapper).selectOne((Wrapper) qw);
    }

    @Override
    public List<DictExamClass> getExamClassList(DictExamClassDto dto) {
        List<DictExamClass> lsExamClass = new ArrayList<>();
        String commonName = null;
        if ("1".equals(dto.getIsGreenChannel())) {
            commonName = getCommonName(dto.getExamClass());
        }
        Boolean isQueryAll = true;
        if (StringUtils.isNotBlank(dto.getUserId()) && StringUtils.isNotBlank(dto.getModuleName())) {
            isQueryAll = false;
            // String functions = Objects.requireNonNull(ConstantsUtil.ModuleFunction.get(dto.getModuleName())).value;
            // List<String> functionList = CommonUtil.split2List(functions);
            DictRoleFunctionDto dictRoleFunctionDto = new DictRoleFunctionDto();
            // dictRoleFunctionDto.setFunctionList(functionList);
            dictRoleFunctionDto.setUserId(dto.getUserId());
            if (StringUtils.isNotBlank(commonName)) {
                dictRoleFunctionDto.setCommonName(commonName);
            } else {
                dictRoleFunctionDto.setExamClass(dto.getExamClass());
            }
            dictRoleFunctionDto.setClassAttr(dto.getClassAttr());
            lsExamClass = dictRoleFunctionService.getFunctionExamClass(dictRoleFunctionDto);
            for (DictExamClass dictExamClass : lsExamClass) {
                if (dictExamClass == null || StringUtils.isBlank(dictExamClass.getExamClassName())) {
                    isQueryAll = true;
                    break;
                }
            }
        }
        if (isQueryAll) {
            DictExamClassDto dictExamClassDto = new DictExamClassDto();
            dictExamClassDto.setClassAttr(dto.getClassAttr());
            dictExamClassDto.setCommonName(commonName);
            dictExamClassDto.setExamClassName(dto.getExamClassName());
            lsExamClass = getList(dictExamClassDto);
        }
        if (StringUtils.isBlank(dto.getSearchAll()) || !dto.getSearchAll().equals("1")) {
            List<String> notExamClassList = getNotExamClassList();
            lsExamClass.removeIf(dec -> notExamClassList.contains(dec.getExamClassName()));
        }
        lsExamClass.removeIf(dec -> {
            Boolean bool = (StringUtils.isNotBlank(dec.getHospitalCode()) && StringUtils.isNotBlank(dto.getHospitalCode())
                    && !dec.getHospitalCode().contains(dto.getHospitalCode()));
            return bool;
        });
        return lsExamClass;
    }

    public String getCommonName(String examClass) {
        DictExamClassDto dto = new DictExamClassDto();
        dto.setExamClassName(examClass);
        DictExamClass dictExamClass = getInfo(dto);
        return (dictExamClass != null) ? dictExamClass.getCommonName() : null;
    }


    @Override
    public List<String> getExamClass(String examClass) {
        List<String> examClasses = new ArrayList<>();
        if (StringUtils.isNotBlank(examClass)) {
            QueryWrapper<DictExamClass> query = new QueryWrapper();
            query.select("EXAM_CLASS_NAME");
            ((query.in("EXAM_CLASS_NAME", CommonUtil.split2List(examClass))).or())
                    .in("EXAM_CLASS_CODE", CommonUtil.split2List(examClass));
            List<DictExamClass> list = baseMapper.selectList(query);
            if (list != null && list.size() > 0) {
                for (DictExamClass dec : list) {
                    examClasses.add(dec.getExamClassName());
                }
            }
        }
        return examClasses;
    }

    public List<String> getNotExamClassList() {
        DictSysparamsDto dto = new DictSysparamsDto();
        dto.setParamName(ConstantsUtil.ConfigName.NOT_EXAM_CLASS.getKey());
        DictSysparamsDto dictSysparamsDto = dictSysparamsMapper.getSysparmsInfo(dto);
        String notExamClass = null;
        if (dictSysparamsDto.getDataValue() != null) {
            notExamClass = dictSysparamsDto.getDataValue();
        } else {
            notExamClass = dictSysparamsDto.getParamValue();
        }
        List<String> list = new ArrayList<String>();
        if (StringUtils.isNotBlank(notExamClass)) {
            list = CommonUtil.split2List(notExamClass);
        }
        return list;
    }
}
